﻿//===============================================================================
// Apolineo Patterns & Practices LeanEngine Framework
//===============================================================================
// Copyright © Apolineo Desenvolvimento de Sistemas Ltda.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================

using System;

using LeanEngine.Extensions;
using LeanEngine.Exception;
using LeanEngine.Validation;
using System.Text;

namespace LeanEngine.SalesConcept.Validator
{
    /// <summary>
    /// Atributo de validação que verifica se a data é igual ou maior
    /// a data do dia
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public sealed class IsNowYearOrLaterAttribute : ValidationAttribute
    {
        #region Constructor Members

        /// <summary>
        /// Initialize a new instance with an error message obtained from a resource.
        /// </summary>
        /// <param name="resourceType">Type of the resource manager that contain the error message</param>
        /// <param name="errorKey">Resource item key for the error message</param>
        public IsNowYearOrLaterAttribute(Type resourceType, string errorKey)
            : base(resourceType, errorKey) { }

        /// <summary>
        /// Initialize a new instance with an error message.
        /// </summary>
        /// <param name="errorMessage">The error message displayed for non valid values.</param>
        public IsNowYearOrLaterAttribute(string errorMessage)
            : base(errorMessage) { }

        #endregion

        #region ValidationAttribute Members

        public override bool Validate(object value)
        {
            //Aqui são usados os métodos de extensão do namespace LeanEngine para compor a validação
            if (value.IsNotNull() && value.ToInt32().IsLessThan(DateTime.Now.Year))
            {
                //A classe 'BusinessException' é utilizada para exceções negociais
                //A propriedade "ErrorMessage" está presente na classe base 'ValidationAttribute'
                //e retorna a mesagem de erro definida no uso do atributo
                throw new BusinessException(string.Format(this.ErrorMessage, DateTime.Now.Year.ToString()));
            }

            return true;
        }

        public override void Validate(object value, StringBuilder errorMessageBuilder)
        {
            if (value.IsNotNull() && value.ToInt32().IsLessThan(DateTime.Now.Year))
            {
                errorMessageBuilder.Append(string.Format(this.ErrorMessage, DateTime.Now.Year.ToString()));
                errorMessageBuilder.Append(Environment.NewLine);
            }
        }

        #endregion
    }
}
